﻿using System;
using System.Web.Http;
using yiCommerce.Core;
using yiCommerce.Core.Domain.Addresses;
using yiCommerce.Models.Checkouts;
using yiCommerce.Models.Commons;
using yiCommerce.Service.Addresses;
using yiCommerce.Service.Customers;

namespace yiCommerce.Controllers.api
{
    public class NewShippingAddressResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public int Id { get; set; }
    }


    public class CheckOutApiController : ApiController
    {

        private ICustomerService _customerService;
        private IAddressService _addressService;
        private IWorkContext _workContext;

        public CheckOutApiController(ICustomerService customerService, IAddressService addressService, IWorkContext workContext)
        {
            _customerService = customerService;
            _addressService = addressService;
            _workContext = workContext;
        }

        public NewShippingAddressResult NewShippingAddress(AddressModel model)
        {

            var result = new NewShippingAddressResult();
            try
            {
                var newAddress = new Address()
                    {
                        Address1 = model.Address1,
                        Address2 = model.Address2,
                        City = model.City,
                        CreatedOnUtc = DateTime.Now,
                        Email = model.Email,
                        FirstName = model.FirstName,
                        Id = model.Id,
                        LastName = model.LastName,
                        MobileNumber = model.MobileNumber,
                        PhoneNumber = model.PhoneNumber,
                        Province = model.Province,
                        ZipPostalCode = model.ZipPostalCode,
                    };

                _addressService.InsertNewAddress(newAddress);
                var customer = _customerService.GetCustomerById(_workContext.CurrentCustomer.Id);
                customer.Addresses.Add(newAddress);

                _customerService.UpdateCustomer(customer);

                result.Status = "0";
                result.Id = newAddress.Id;
            }
            catch (Exception e)
            {
                result.Status = "1";
                result.Message= e.Message;
            }
          
            return result;
        }
    }
}
